package com.itheima.controller;


import com.github.pagehelper.Page;
import com.itheima.entity.Result;
import com.itheima.entity.ResultMessage;
import com.itheima.pojo.PictureSort;
import com.itheima.pojo.Todo;
import com.itheima.service.PictureSortService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 图片分类表 前端控制器
 * </p>
 *
 * @author wangchang
 * @since 2020-08-04
 */
@RestController
@RequestMapping("/pictureSort")
@CrossOrigin(allowCredentials="true", allowedHeaders="*", methods={RequestMethod.GET,RequestMethod.POST}, origins="*")
public class PictureSortController {
    @Autowired
    private PictureSortService pictureSortService;
    
    // 增加图片分类
    @RequestMapping("/add")
    public Result addPictureSort(@RequestBody PictureSort pictureSort) {
        String addResult = pictureSortService.add(pictureSort);
        if ("failed".equals(addResult)) {
            return new Result("error", "插入失败");
        }

        return new Result("success","插入成功");
    }
    // 删除图片分类
    @RequestMapping("/delete")
    public Result deletePictureSort(@RequestBody PictureSort pictureSort) {
        String deleteResult = pictureSortService.delete(pictureSort);
        if ("failed".equals(deleteResult)) {
            return new Result("error", "删除失败");
        }
        return new Result("success","删除成功");
    }
    // 编辑图片分类
    @RequestMapping("/edit")
    public ResultMessage editPictureSort(@RequestBody PictureSort pictureSort) {
        String editResult = pictureSortService.eidt(pictureSort);
        if ("failed".equals(editResult)) {
            return new ResultMessage(0,"error","操作失败");
        }
        return new ResultMessage(1,"success","操作成功");
    }

    // 获取图片分类列表
    @PostMapping("/getList")
    public Result getLisTodo(@RequestBody Map<String, Object> searchMap) {
        Page<PictureSort> pageList = pictureSortService.getPageList(searchMap);
        com.itheima.entity.Page page = new com.itheima.entity.Page();

        page.setCurrent((Integer) searchMap.get("currentPage"));
        page.setRecords(pageList.getResult());
        page.setTotal((int) pageList.getTotal());

        return new Result("success", page);
    }
    
    // 通过Uid获取分类
    @RequestMapping("/getPictureSortByUid")
    public Result getPictureSortByUid(@RequestBody PictureSort pictureSort) {
        String uid = pictureSort.getUid();
        
        PictureSort pictureSort1 = pictureSortService.findPictureSortByUid(uid);
        if (pictureSort1 == null) {
            return new Result("error","查询失败");
        }
        return new Result("success",pictureSort);
    }

    // 置顶分类
    @RequestMapping("/stick")
    public Result stickPictureSort(@RequestBody PictureSort pictureSort) {
        String editResult = pictureSortService.eidt(pictureSort);
        if ("failed".equals(editResult)) {
            return new Result("error","查询失败");
        }
        return new Result("success","置顶成功");
    }

}

